home *** CD-ROM | disk | FTP | other *** search
- /*******************************<+>***************************
- ** DTA
- *************************************************************
- **
- ** $Id: Array.h,v 1.8 1994/03/01 23:01:19 dta Exp $
- **
- ** $Source: /cvs/lib/odmg/Array.h,v $
- **
- ** What @(#):
- **
- ** Author: Dale T. Anderson
- **
- *******************************<+>***************************/
-
- #ifndef _Array_h
- #define _Array_h
-
- #include <Odmg/Collection.h>
-
- template <class T> class Varray;
-
- template <class T>
- class Varray :
- public Collection <T>
- {
- Position m_total; // total number of elements allocated
- int m_chunk; // number of elements to expand by
-
- Ref <T> *m_array;
-
- virtual Ref <Collection <T> > create (void) const;
-
- Varray (const Ref <Varray <T> >);
-
- public:
-
- Varray (Position length, const int chunk = 10);
- ~Varray ();
-
- Ref <T> operator [] (const Position);
- void remove_element_at (const Position);
- void replace_element_at (Ref <T>, const Position);
- virtual Ref <T> retrieve_element_at (const Position);
- void resize (Position length);
- int find_element (const Ref <T>) const;
- int upper_bound (void) const;
-
- //
- // Collection methods
- //
-
- virtual void insert_element (Ref <T>);
- virtual void remove_element (Ref <T>);
-
- virtual void replace_element_at (Ref <T>, const Iterator <T> &);
- };
-
- template <class T>
- Varray<T>::Varray (Position length, const int chunk) :
- m_total (length),
- m_chunk (chunk)
- {
- if (length)
- m_array = new Ref <T> [length];
- else
- m_array = NULL;
- }
-
- template <class T>
- Varray<T>::~Varray ()
- {
- delete m_array;
- }
-
- template <class T>
- Ref <T> Varray<T>::operator [] (const Position position)
- {
- return retrieve_element_at (position);
- }
-
- template <class T>
- void Varray<T>::remove_element_at (const Position position)
- {
- assert (position >= 0);
- assert (position < cardinality ());
-
- for (Position i = position; i < m_count - 1; i++)
- m_array [i] = m_array [i + 1];
-
- m_array [--m_count] = 0;
-
- if (m_total - m_count > m_chunk * 2)
- resize (m_total - m_chunk * 2);
- }
-
- template <class T>
- void Varray<T>::replace_element_at (Ref <T> element, const Position position)
- {
- assert (position >= 0);
- assert (position < cardinality ());
- m_array [position] = element;
- }
-
- template <class T>
- Ref <T> Varray<T>::retrieve_element_at (const Position position)
- {
- assert (position >= 0);
- assert (position < cardinality ());
- return m_array [position];
- }
-
- template <class T>
- void Varray<T>::resize (Position length)
- {
- if (length) {
- Ref <T> *array = new Ref <T> [length];
- for (int i = 0; i < length &&i < m_total; i++)
- array [i] = m_array [i];
- while (i < length)
- array [i++] = NULL;
- delete m_array;
- m_array = array;
-
- if (length < m_count)
- m_count = length;
- } else {
- delete m_array;
- m_array = NULL;
- m_count = 0;
- }
- m_total = length;
- }
-
- template <class T>
- int Varray<T>::find_element (const Ref <T> ref) const
- {
- for (int i = 0; i < m_count; i++)
- if (ref == m_array [i])
- return i;
- return -1;
- }
-
- template <class T>
- int Varray<T>::upper_bound () const
- {
- return m_total;
- }
-
- //
- // Collection methods
- //
-
- template <class T>
- void Varray<T>::insert_element (Ref <T> ref)
- {
- if (m_count == m_total)
- resize (m_total + m_chunk);
-
- m_array [m_count++] = ref;
- }
-
- template <class T>
- void Varray<T>::remove_element (Ref <T> ref)
- {
- remove_element_at (find_element (ref));
- }
-
- template <class T>
- void Varray<T>::replace_element_at (Ref <T> ref, const Iterator <T> &iter)
- {
- m_array [iter.get_position ()] = ref;
- }
-
- template <class T>
- Varray<T>::Varray (const Ref <Varray <T> > array) :
- m_chunk (array->m_chunk),
- m_total (0),
- m_array (NULL)
- {
- m_is_ordered = array->ordered ();
- m_allows_duplicates = array->allows_duplicates ();
- }
-
- template <class T>
- Ref <Collection <T> > Varray <T>::create (void) const
- {
- return Ref <Collection <T> > (new Varray <T> (this));
- }
-
- #endif
-